package com.bw.day0713.demo.src.main.java.com.bw.controller;

import com.bw.bean.Student;
import com.bw.server.UserServer;
import com.bw.utils.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class UserController {


    @Autowired //他就是到内存中找对象
    UserServer userServer;

    @GetMapping("/findAll")
    public List<Map> findAll(){
        return userServer.findAll();
    }


    @GetMapping("/findById")
    public Student findById(Integer id){
        return userServer.findById(id);
    }

    @RequestMapping("/getUv")
    public String getUv(@RequestParam(defaultValue = "1") Integer dt){

        if (dt == 1){
            dt = DateUtil.now();
        }


        List<Map> list = userServer.findTrafficUv(dt);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":");
        ArrayList<String> keyList = new ArrayList<>();
        ArrayList<Integer> valueList = new ArrayList<>();
        for (Map map : list) {
            keyList.add(map.get("ch").toString());
            valueList.add(Integer.valueOf(map.get("uv").toString()));
        }

        stringBuffer.append("[\"");
        stringBuffer.append(StringUtils.join(keyList,"\",\""));
        stringBuffer.append("\"]");

        stringBuffer.append(",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"手机品牌\",\n" +
                "        \"data\":");

        stringBuffer.append(valueList.toString());

        stringBuffer.append("}\n" +
                "    ]\n" +
                "  }\n" +
                "}");


        return stringBuffer.toString();
    }



    @RequestMapping("/findUb")
    public String findUb() {
        try {
            List<Map> list = userServer.findUb();

            // 构建结果Map
            Map<String, Object> result = new HashMap<>();
            result.put("status", 0);
            result.put("msg", "");

            // 构建data部分
            Map<String, Object> data = new HashMap<>();
            data.put("total", list.size());

            // 定义列信息
            data.put("columns", Arrays.asList(
                    createColumn("用户ID", "uid"),
                    createColumn("宝宝性别", "gender"),
                    createColumn("年龄段", "age")
            ));

            // 定义表头分组
            data.put("superHeaders", Collections.singletonList(
                    Arrays.asList(
                            createSuperHeader("用户", 1),
                            createSuperHeader("标签", 2)
                    )
            ));

            // 设置行数据
            data.put("rows", list);

            // 将data放入结果
            result.put("data", data);

            // 使用Jackson序列化Map为JSON字符串
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);

        } catch (Exception e) {
            // 异常处理
            e.printStackTrace();
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("status", 500);
            errorResult.put("msg", "生成数据失败: " + e.getMessage());
            errorResult.put("data", Collections.emptyMap());




            try {
                ObjectMapper mapper = new ObjectMapper();
                return mapper.writeValueAsString(errorResult);
            } catch (Exception ex) {
                return "{\"status\":500,\"msg\":\"系统错误\",\"data\":{}}";
            }
        }
    }

    // 辅助方法：创建列定义
    private Map<String, String> createColumn(String name, String id) {
        Map<String, String> column = new HashMap<>();
        column.put("name", name);
        column.put("id", id);
        return column;
    }

    // 辅助方法：创建超级表头
    private Map<String, Object> createSuperHeader(String name, int colspan) {
        Map<String, Object> header = new HashMap<>();
        header.put("name", name);
        header.put("colspan", colspan);
        return header;
    }





    @RequestMapping("/test")
    public String test(){
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      \"苹果111\",\n" +
                "      \"三星\",\n" +
                "      \"华为\",\n" +
                "      \"oppo\",\n" +
                "      \"vivo\",\n" +
                "      \"小米13\"\n" +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"手机品牌\",\n" +
                "        \"data\": [\n" +
                "          9901,\n" +
                "          6094,\n" +
                "          6181,\n" +
                "          9928,\n" +
                "          6784,\n" +
                "          7349\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }






}
